package ws.jonas.common.config;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ws.jonas.common.log.Logger;

public class ConfigParser extends DefaultHandler {
	private String currentElement;
	private int updates = 0;
	private static final Logger log = Logger.getInstance(ConfigParser.class);
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) {
		currentElement = qName;
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String data = new String(ch, start, length).trim();
		if (data == null || data.equals("")) {
			return;
		}
		
		if (Config.isValid(currentElement)) {
			++updates;
			Config.set(currentElement, data);			
		} else {
			log.warning("Unknown config tag: " + currentElement);
		}
	}
	
	public int getUpdateCount() {
		return updates;
	}
}
